import { createSlice } from '@reduxjs/toolkit';

//定义一个接口
interface CountState{
  value: number;
}

//定义初始值
let initialState: CountState = {
  value: 0
}

//创建 slice 对象
let slice = createSlice({
  name: 'count', // 定义slice的名称
  initialState: initialState, // 定义slice的初始状态
  reducers: {
    add: (state, action) => { // 定义一个名为add的reducer
      state.value += action.payload;
    },
    sub: (state, action) => { // 定义一个名为sub的reducer
      state.value -= action.payload;
    },
  },
});

//获得 action creator
export const { add, sub } = slice.actions;

// 获得reducer函数
const reducer = slice.reducer;

//暴露 reducer
export default reducer;